﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Entities;
using MM.Business; 
namespace MM
{
    public partial class frmTimPhieuDH : Form
    {
        string _sConn = "";
        PHIEU_DAT_HANG oPDH = new PHIEU_DAT_HANG();

        // the delegate the subscribers must implement
        public delegate void ChonPhieuDatHang(object frmTimPhieuYC_KetQua, ChonPhieuDHEventArgs chonDHCevent);
        // an instance of the delegate
        public ChonPhieuDatHang chonPDH;

        public frmTimPhieuDH(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
        }

        private void frmTimPhieuDH_Paint(object sender, PaintEventArgs e)
        {
            Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            oPDH.POThamChieuSAP = this.txtMaPhieuYeuCau.Text.Trim();
            oPDH.NguoiNhap = this.txtNguoiThucHien.Text.Trim();
            oPDH.NgayNhap = this.dateEditNgayGiaoHang.DateTime;
            oPDH.NgayNhapTo = this.dateEditNgayGiaoHangTo.DateTime;

            int isnhapkho = 2;

            if (radioButton1.Checked == true)
            {
                isnhapkho = 1;
            }
            if (radioButton2.Checked == true)
            {
                isnhapkho = 0;
            }
            if (radioButton4.Checked == true)
            {
                oPDH.TinhTrang = 1;
            }

            List<PHIEU_DAT_HANG> lst = new List<PHIEU_DAT_HANG>();
            clsPHIEU_DAT_HANG pdhCtrl = new clsPHIEU_DAT_HANG(_sConn);
            lst = pdhCtrl.SelectPHIEU_DAT_HANGsSearch(oPDH, isnhapkho);
            if (lst.Count == 0)
                MessageBox.Show("KHÔNG TÌM THẤY PHIẾU ĐẶT HÀNG THỎA ĐIỀU KIỆN");
            else
            {
                this.Hide();
                // GOI FORM KET QUA TIM KIEM                
                frmTimPhieuDH_KetQua f = new frmTimPhieuDH_KetQua( _sConn,lst);
                f.chonPDH += new frmTimPhieuDH_KetQua.ChonPhieuDatHang(GetPhieuDatHang);
                f.Location = new Point(
                    (this.Location.X + this.Width / 2 + 100) - (f.Width / 2),
                    (this.Location.Y + this.Height / 2 + 100) - (f.Height / 2));
                f.StartPosition = FormStartPosition.Manual;
                f.Show();

                
                this.Close();
            }

        }

        private void GetPhieuDatHang(object sender, ChonPhieuDHEventArgs e)
        {
            

            ChonPhieuDHEventArgs pycevent = new ChonPhieuDHEventArgs(e.POThamChieuSAP);

            if (chonPDH != null)
            {
                chonPDH(this, pycevent);
            }
        }

    }
}